<template>
    <div class="carousel">
        <Button :children="children.prev" :disabled="count === 0" @button-click="prevClick"></Button>
        <div class="carousel-content">
            <div class="contentList">
                <div class="contentItem" v-for="(item, index) in data" :key="index">
                    <h3>{{ item.title }}</h3>
                    <p>{{ item.text }}</p>
                </div>
            </div>
        </div>
        <div class="phone">
            <div class="phoneList">
                <div class="phoneItem" v-for="(item, index) in data" :key="index">
                    <img v-for="i in item.list" :src="i">
                </div>
            </div>
        </div>
        <div class="phoneMark">

        </div>
        <Button :children="children.next" :disabled="count === data.length - 1" @button-click="nextClick"></Button>
    </div>
</template>
<script setup>
import { onMounted, reactive, ref, shallowRef, watch } from 'vue';
import Button from './header/Button.vue';
import { gsap } from 'gsap';

const data = reactive([
    {
        title: 'Control & Manage',
        text: 'SPARQ makes it easy for you to understand your vehicle with our Comprehensive System Rating',
        list: [
            '/carousel01_01.png',
            '/carousel01_02.png',
            '/carousel01_03.png',
            '/carousel01_04.png',
        ],
    },
    {
        title: 'Always Be In The Know',
        text: "As your car's needs change, so does your score with our Dynamic A.I. Car Health Scoring.",
        list: ['/carousel02_01.png', '/carousel02_02.png', '/carousel02_03.png'],
    },
    {
        title: 'Drive with Confidence',
        text: "Our Error Code Categorization makes it simple to know what's critical and what can wait.",
        list: ['/carousel03_01.png', '/carousel03_02.png', '/carousel03_03.png'],
    }
])
const children = reactive({
    prev: "Prev",
    next: "Next"
})
const count = ref(0)
const timeLine = shallowRef(null)
const contentItems = ref([])
const phoneItems = ref([])

onMounted(() => {
    contentItems.value = gsap.utils.toArray('.contentItem')
    phoneItems.value = gsap.utils.toArray('.phoneItem')

    timeLine.value = gsap.timeline({
        defaults: {
            duration: 0.8,
            ease: 'power2.inOut'
        }
    })
})

const carGsap = (count) => {
    timeLine.value.to(".contentList", {
        xPercent: -100 * count
    }).to(contentItems.value, {
        opacity: 0,
    }, "<").to(contentItems.value[count], {
        opacity: 1,
    }, "<").to(".phoneList", {
        xPercent: -100 * count
    }, "<").fromTo(phoneItems.value[count],{
        alpha:0,
        rotationX:90,
        rotationY:50,
    },{
        alpha:1,
        rotationX:0,
        rotationY:0,
        stagger:0.2,
    },"<=0.2")
}

watch(count, (newCount) => {
    if (timeLine.value) carGsap(newCount)
})

const prevClick = () => {
    count.value--
}
const nextClick = () => {
    count.value++
}


</script>
<style scoped>
.carousel {
    display: flex;
    align-items: center;
    padding: 100px 50px;
    position: relative;
}

.carousel-content {
    flex: 1;
    overflow: hidden;
    margin: 0 50px;
}

.contentList {
    display: flex;
}

.contentItem {
    width: 100%;
    flex-shrink: 0;

    h3 {
        width: 35%;
        font-size: 3.5vw;
    }

    p {
        width: 40%;
        font-size: 1.5vw;
        margin-top: 100px;
    }
}

.phone {
    position: absolute;
    top: 100px;
    right: 20%;
    width: 14vw;
    aspect-ratio: 9 / 16;
    z-index: 10;
    overflow: hidden;
    border-radius: 30px;
}

.phoneList {
    width: 100%;
    height: 100%;
    display: flex;
}

.phoneItem {
    width: 100%;
    flex-shrink: 0;
    background-size: cover;
    padding-top: 5vw;
    padding-left: 1vw;
    padding-right: 1vw;
    box-sizing: border-box;

    img {
        width: 100%;
        margin-bottom: 2vw;
    }
}

.phoneItem:nth-of-type(1) {
    background-image: url('/carousel01.png');

}

.phoneItem:nth-of-type(2) {
    background-image: url('/carousel02.png');

}

.phoneItem:nth-of-type(3) {
    background-image: url('/carousel03.png');

}

.phoneMark {
    position: absolute;
    top: 100px;
    right: 20%;
    width: 14vw;
    aspect-ratio: 9 / 16;
    z-index: 20;
    background-image: url('/carouselphone.png');
    background-size: 100% 97%;
    background-repeat: no-repeat;
    transform: scale(1.1);
    transform-origin: center top;
}
</style>